<?php
/**
*	部门
*	访问地址如：http://127.0.0.1/app/rockreim/api.php?m=opendept
*/
class opendeptClassAction extends openapicompanyAction
{
	/**
	*	获取所有部门
	*/
	public function deptlistAction()
	{
		$rows = m('dept')->getDept($this->companyid);
		return returnsuccess(array(
			'deptlist'	=> $rows
		));
	}
	
	/**
	*	更新所有部门
	*/
	public function deptallupdateAction()
	{
		$garr	= $this->getbody();
		if(!$garr['success'])return $garr;
		$cid 	= $this->companyid;
		$rows 	= arrvalue($garr['data'], 'deptalldata');
		if(!$rows)return returnerror('没有数据');
		$ids 	= '1';
		$dbs 	= m('dept');
		foreach($rows as $k=>$rs){
			$where = '`cid`='.$cid.' and `id`='.$rs['id'].'';
			$uarr  = array();
			$uarr['cid'] 	= $cid;
			$uarr['id'] 	= $rs['id'];
			$uarr['name'] 	= $rs['name'];
			if(!isempt($rs['num']))$uarr['num']  	= $rs['num'];
			if(!isempt($rs['headman']))$uarr['headman']  	= $rs['headman'];
			$uarr['status']  = 1;
			$uarr['optdt']   = $this->now;
			$uarr['sort'] 	 = (int)$rs['sort'];
			$uarr['pid'] 	 = (int)$rs['pid'];
			if($dbs->rows($where)==0)$where = '';
			$bo = $dbs->record($uarr, $where);
			if(!$bo)return returnerror($this->db->error());
			$ids.=','.$rs['id'].'';
		}
		$dbs->delete('`cid`='.$cid.' and `id` not in('.$ids.')');

		return returnsuccess();
	}
	
	
	
	
	/**
	*	获取所有用户
	*/
	public function useralllistAction()
	{
		$rows = m('usera')->getList($this->companyid,'`user`,`name`,`position`,`mobile`,`mobilecode`,`tel`,`status`,`email`,`deptid`,`gender`,`sort`,`pingyin`');
		return returnsuccess(array(
			'userlist'	=> $rows
		));
	}
	
	/**
	*	用户信息
	*/
	public function userinfoAction()
	{
		$user = $this->get('user');
		$urs  = m('usera')->getone("`cid`='{$this->companyid}' and `user`='$user'",'`user`,`name`,`uid`,`position`,`mobile`,`tel`,`mobilecode`,`status`,`email`,`deptid`,`gender`,`sort`,`pingyin`');
		if(!$urs)return returnerror('用户不存在', 404);
		$uid  = $urs['uid'];
		$urs['face'] = getface('');
		if($uid>0){
			$uurs = m('users')->getone($uid);
			if($uurs){
				$urs['face'] = getface($uurs['face']);
			}
		}
		//unset($urs['uid']);
		return returnsuccess(array(
			'userinfo'	=> $urs
		));
	}
	
	/**
	*	用户更新
	*/
	public function userupdateAction()
	{
		$garr	= $this->getbody();
		if(!$garr['success'])return $garr;
		$cid 	= $this->companyid;
		$da 	= $garr['data'];
		$user 		= arrvalue($da,'user');
		$name 		= arrvalue($da,'name');
		if(isempt($user))return returnerror('用户名不能为空');
		if(isempt($name))return returnerror('姓名不能为空');
		$dbs  = m('usera');
		$urs  = $dbs->getone("`cid`='{$cid}' and `user`='$user'");
		$uarr['name'] = $name;
		$uarr['user'] = $user;
		$uarr['cid']  = $cid;
		$uarr['optdt'] = $this->now;
		$ids 	= 0;
		if(!$urs){
			$uarr['deptid'] = 1;
			$uarr['createdt'] = $this->now;
			$uarr['position'] = arrvalue($da,'position');
		}else{
			$ids = $urs['id'];
		}
		if(isset($da['deptid']))$uarr['deptid'] = (int)$da['deptid'];
		if(isset($da['position']))$uarr['position'] = $da['position'];
		if(isset($da['sort']))$uarr['sort'] = $da['sort'];
		if(isset($da['gender']))$uarr['gender'] = $da['gender'];
		if(isset($da['pingyin']))$uarr['pingyin'] = $da['pingyin'];
		if(isset($da['email']))$uarr['email'] = $da['email'];
		if(isset($da['istxl']))$uarr['istxl'] = $da['istxl'];
		if(isset($da['tel']))$uarr['tel'] = $da['tel'];
		if(isset($da['superman']))$uarr['superman'] = $da['superman'];
		if(isset($da['mobile'])){
			$mobile 	= $da['mobile'];
			if($ids==0 || $urs['uid']==0){
				$uarr['mobile'] = $mobile;
				$tus = $dbs->getone("`cid`='{$cid}' and `mobile`='$mobile' and `id`<>'$ids'");
				if($tus)return returnerror('此手机号已在“'.$tus['name'].'”已存在');
			}
		}
		if(isset($uarr['deptid'])){
			$did = $uarr['deptid'];
			$drs = m('dept')->getone('`cid`='.$cid.' and `id`='.$did.'');
			if(!$drs)return returnerror('部门('.$did.')不存在，请先同步部门');
			$uarr['deptname'] = $drs['name'];
		}
		if($ids==0){
			$bo = $dbs->insert($uarr);
		}else{
			$bo = $dbs->update($uarr,$ids);
		}
		if(!$bo)return returnerror($this->db->error());
		
		return returnsuccess();
	}
	
	
	/**
	*	删除用户
	*/
	public function userdeleteAction()
	{
		$user = $this->get('user');
		if(isempt($user)){
			$garr	= $this->getbody();
			if(!$garr['success'])return $garr;
			$user  	= arrvalue($garr['data'], 'userlist');
		}
		if(isempt($user))return returnerror('没有设置用户名可删除');
		$users = "'".str_replace(',',"','", $user)."'";
		m('usera')->delete("`cid`='{$this->companyid}' and `user` in($users) and `type`<>2");
		return returnsuccess();
	}
}	